//
//  BanBu_CollectionImageView.m
// Halfarea
//
//  Created by Wang Xj on 14-4-8.
//
//

#import "BanBu_CollectionImageView.h"
#import "UIImageView+WebCache.h"
#import "SCGIFImageView.h"
#import "BanBu_SmileViewAPI.h"
#import "AppCommunicationManager.h"
#import "AppDataManager.h"

@implementation BanBu_CollectionImageView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
     

    }
    return self;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:KSetAddButtonHidden object:nil];
    [_imageInfoDic release],_imageInfoDic = nil;
    [super dealloc];
}

//设置图片各属性
-(void)setImageInfo:(NSDictionary *)imageInfo
{
    self.imageInfoDic = imageInfo;
    if (imageInfo==nil)
    {
        self.image = nil;
        self.imageInfoDic = nil;
        self.userInteractionEnabled = NO;
        return;
    }
    
    //添加手势
    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(showBigImage:)];
    [self addGestureRecognizer:longPress];
    [longPress release];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedImage)];
    [self addGestureRecognizer:tap];
    [tap release];
    
    self.userInteractionEnabled = YES;
    
    BOOL hidden = NO;
    
    if ([[imageInfo valueForKey:KeyName] isEqualToString:@"20140101000000"] && [[imageInfo valueForKey:KeyPath] isEqualToString:@"localhost"])
    {
        if ([self.delegate respondsToSelector:@selector(getIsEditing)])
        {
            BOOL editing = [self.delegate getIsEditing];
            
            if (editing)
            {
               hidden = YES;
            }
            if (self.delegate || [self.delegate respondsToSelector:@selector(setAddButtonViewHidden: withBool:)])
            {
                [self.delegate setAddButtonViewHidden:self withBool:hidden];
            }
        }
        //这是最后一个按钮，显示为添加按钮
        self.image = [UIImage imageNamed:@"btn_collect_add.png"];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setAddButtonHidden:) name:KSetAddButtonHidden object:nil];
        
        return;
    }
    else
    {
        hidden = NO;
        if (self.delegate || [self.delegate respondsToSelector:@selector(setAddButtonViewHidden: withBool:)])
        {
            [self.delegate setAddButtonViewHidden:self withBool:hidden];
        }
    }
    

    
    NSString * imageName = [_imageInfoDic valueForKey:KeyName];
    NSString * imageUrl = [_imageInfoDic valueForKey:KeyPath];
    
    [self setImageWithUrlString:imageUrl withFolderName:CollectionFolder withImageName:imageName];
}

-(void)showBigImage:(UILongPressGestureRecognizer *)longPress
{
    switch (longPress.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            if (self.delegate || [self.delegate respondsToSelector:@selector(showBigImage:)])
            {
                [self.delegate showBigImage:self];
            }
        }
            break;
            
        default:
            break;
    }
}

-(void)selectedImage
{
    if ([[self.imageInfoDic valueForKey:KeyPath] isEqualToString:@"localhost"])
    {
        //添加图片
        if (self.delegate || [self.delegate respondsToSelector:@selector(setSelectedView:)])
        {
            [self.delegate addCollectedEmo:self];
        }
        return;
    }
    if (self.delegate || [self.delegate respondsToSelector:@selector(setSelectedView:)])
    {
        [self.delegate setSelectedView:self];
    }
}

-(void)setAddButtonHidden:(NSNotification *)notifi
{
    if ([[self.imageInfoDic valueForKey:KeyName] isEqualToString:@"20140101000000"] && [[self.imageInfoDic valueForKey:KeyPath] isEqualToString:@"localhost"])
    {
        BOOL hidden = [[notifi.object valueForKey:@"hidden"] boolValue];
        
        if (self.delegate || [self.delegate respondsToSelector:@selector(setAddButtonViewHidden: withBool:)])
        {
            [self.delegate setAddButtonViewHidden:self withBool:hidden];
        }
    }
}

@end
